package cn.tedu.tea.service;

import cn.tedu.tea.domain.GoodsCategory;
import cn.tedu.tea.domain.param.GoodsCategoryAddNewDTO;
import cn.tedu.tea.domain.param.GoodsCategoryUpdateDTO;
import cn.tedu.tea.domain.vo.GoodsCategoryListVO;
import cn.tedu.tea.domain.vo.GoodsCategoryStandardVO;
import cn.tedu.tea.ex.ServiceException;
import cn.tedu.tea.mapper.GoodsCategoryMapper;
import cn.tedu.tea.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class GoodsCategoryServiceImpl implements IGoodsCategoryService {

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Override
    public void addNew(GoodsCategoryAddNewDTO goodsCategoryAddNewDTO) {
        log.debug("开始处理【添加商品类别】的业务，参数： {}", goodsCategoryAddNewDTO);
        int count = goodsCategoryMapper.countByName(goodsCategoryAddNewDTO.getName());
        if (count > 0){
            String message = "添加商品类别失败，系统文件名已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        GoodsCategory goodsCategory = new GoodsCategory();
        BeanUtils.copyProperties(goodsCategoryAddNewDTO, goodsCategory);
        goodsCategory.setModifiedUserId(0).setModifiedUserName("");
        int rows = goodsCategoryMapper.insert(goodsCategory);
        if (rows != 1){
            String message = "添加商品类别失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Integer id) {
        log.debug("开始处理【根据id删除商品类别】的业务，参数： {}", id);
        GoodsCategoryStandardVO goodsCategoryStandard = goodsCategoryMapper.getStandardById(id);
        if (goodsCategoryStandard == null){
            String message = "删除商品类别失败，要删除的商品类别不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = goodsCategoryMapper.deleteById(id);
        if (rows != 1){
            String message = "删除商品类别失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        log.debug("开始处理【根据id批量删除商品类别】的业务，参数： {}", ids);
        int rows = goodsCategoryMapper.deleteByIds(ids);
        if (rows < 1){
            String message = "删除商品类别失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(GoodsCategoryUpdateDTO goodsCategoryUpdateDTO) {
        log.debug("开始处理【修改商品类别信息】的业务，参数：{}", goodsCategoryUpdateDTO);
        GoodsCategoryStandardVO goodsCategoryStandard = goodsCategoryMapper.getStandardById(goodsCategoryUpdateDTO.getId());
        if (goodsCategoryStandard == null){
            String message = "修改商品类别失败，要访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int count = goodsCategoryMapper.countByNameAndNotId(goodsCategoryUpdateDTO.getName(), goodsCategoryUpdateDTO.getId());
        if (count > 0){
            String message = "修改商品类别失败，系统文件名已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        GoodsCategory goodsCategory = new GoodsCategory();
        BeanUtils.copyProperties(goodsCategoryUpdateDTO, goodsCategory);
        int rows = goodsCategoryMapper.update(goodsCategory);
        if (rows != 1){
            String message = "修改商品类别失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void setDisplay(Integer id) {
        log.debug("开始处理【将商品类别设置为上架状态】的业务，参数：{}", id);
        updateStatusById(id, 0);
    }

    @Override
    public void setHidden(Integer id) {
        log.debug("开始处理【将商品类别设置为下架状态】的业务，参数：{}", id);
        updateStatusById(id, 1);
    }

    @Override
    public GoodsCategoryStandardVO getStandardById(Integer id) {
        log.debug("开始处理【根据id查询商品类别详情】的业务，参数： {}", id);
        GoodsCategoryStandardVO goodsCategoryStandard = goodsCategoryMapper.getStandardById(id);
        if (goodsCategoryStandard == null){
            String message = "查询商品类别详情失败，要查询的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return goodsCategoryStandard;
    }

    @Override
    public List<GoodsCategoryListVO> list() {
        log.debug("开始处理【查询商品类别列表】的业务，无参数");
        List<GoodsCategoryListVO> list = goodsCategoryMapper.list();
        return list;
    }

    private void updateStatusById(Integer id, Integer status) {
        GoodsCategoryStandardVO currentGoodsCategory = goodsCategoryMapper.getStandardById(id);
        if (currentGoodsCategory == null) {
            String message = STATUS_TEXT[status] + "商品类别失败，商品类别数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        if (currentGoodsCategory.getStatus() == status) {
            String message = STATUS_TEXT[status] + "商品类别失败，此商品类别已经处于" + STATUS_TEXT[status] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        GoodsCategory updateGoodsCategory = new GoodsCategory();
        updateGoodsCategory.setId(id);
        updateGoodsCategory.setStatus(status);
        int rows = goodsCategoryMapper.update(updateGoodsCategory);
        if (rows != 1) {
            String message = STATUS_TEXT[status] + "商品类别失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
}
